文件I/O操作

概述

文件输入输出(I/O)操作是编程中非常重要的部分,它允许程序读取和写入文件。在C#中,文件I/O操作主要通过System.IO命名空间中的类来实现。本教程将详细介绍如何在C#中进行文件的读取和写入操作,并提供相应的代码示例和实践练习。

1. 文件I/O基础

1.1 文件和流

在C#中,文件I/O操作通常涉及两个核心概念:文件和流。

  • 文件:文件是存储在磁盘上的数据集合。文件可以是文本文件、二进制文件等。
  • 流:流是数据在程序和文件之间的传输通道。流可以是输入流(从文件读取数据)或输出流(向文件写入数据)。

1.2 System.IO命名空间

System.IO命名空间提供了许多用于文件和目录操作的类,常用的类包括:

  • File:提供静态方法来创建、复制、删除、移动和打开文件。
  • FileInfo:提供实例方法来创建、复制、删除、移动和打开文件。
  • StreamReader:用于从文件中读取字符数据。
  • StreamWriter:用于向文件中写入字符数据。
  • BinaryReader:用于从文件中读取二进制数据。
  • BinaryWriter:用于向文件中写入二进制数据。

2. 读取文件

2.1 使用StreamReader读取文本文件

StreamReader类用于从文本文件中读取字符数据。以下是一个简单的示例,展示如何使用StreamReader读取文本文件的内容:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = "example.txt";

        // 检查文件是否存在
        if (File.Exists(filePath))
        {
            // 使用 StreamReader 读取文件内容
            using (StreamReader reader = new StreamReader(filePath))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                }
            }
        }
        else
        {
            Console.WriteLine("文件不存在。");
        }
    }
}

2.2 使用BinaryReader读取二进制文件

BinaryReader类用于从二进制文件中读取数据。以下是一个示例,展示如何使用BinaryReader读取二进制文件的内容:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = "example.dat";

        // 检查文件是否存在
        if (File.Exists(filePath))
        {
            // 使用 BinaryReader 读取文件内容
            using (BinaryReader reader = new BinaryReader(File.Open(filePath, FileMode.Open)))
            {
                int intValue = reader.ReadInt32();
                float floatValue = reader.ReadSingle();
                string stringValue = reader.ReadString();

                Console.WriteLine($"Int: {intValue}, Float: {floatValue}, String: {stringValue}");
            }
        }
        else
        {
            Console.WriteLine("文件不存在。");
        }
    }
}

3. 写入文件

3.1 使用StreamWriter写入文本文件

StreamWriter类用于向文本文件中写入字符数据。以下是一个示例,展示如何使用StreamWriter写入文本文件:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = "output.txt";

        // 使用 StreamWriter 写入文件内容
        using (StreamWriter writer = new StreamWriter(filePath))
        {
            writer.WriteLine("Hello, World!");
            writer.WriteLine("This is a test file.");
        }

        Console.WriteLine("文件写入成功。");
    }
}

3.2 使用BinaryWriter写入二进制文件

BinaryWriter类用于向二进制文件中写入数据。以下是一个示例,展示如何使用BinaryWriter写入二进制文件:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = "output.dat";

        // 使用 BinaryWriter 写入文件内容
        using (BinaryWriter writer = new BinaryWriter(File.Open(filePath, FileMode.Create)))
        {
            writer.Write(42); // 写入整数
            writer.Write(3.14f); // 写入浮点数
            writer.Write("Hello, Binary!"); // 写入字符串
        }

        Console.WriteLine("文件写入成功。");
    }
}

4. 实践练习

4.1 练习1:读取并显示文本文件内容

编写一个程序,读取一个文本文件的内容,并将其显示在控制台上。如果文件不存在,程序应提示用户文件不存在。

4.2 练习2:复制文件

编写一个程序,将一个文本文件的内容复制到另一个文件中。确保目标文件不存在时,程序能够创建新文件。

4.3 练习3:记录日志

编写一个程序,将用户的输入记录到一个日志文件中。每次运行程序时,用户的输入应追加到日志文件的末尾。

5. 总结

通过本教程,您已经学习了如何在C#中进行文件的读取和写入操作。我们介绍了StreamReader和StreamWriter用于文本文件的读写,以及BinaryReader和BinaryWriter用于二进制文件的读写。通过实践练习,您可以进一步巩固这些知识。

文件I/O操作是编程中非常基础且重要的技能,掌握这些操作将帮助您更好地处理文件数据,为更复杂的应用程序打下坚实的基础。